<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plcName">
              <a-input v-model="model.plcName" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="ip地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ip">
              <a-input v-model="model.ip" placeholder="请输入ip地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="端口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="port">
              <a-input-number v-model="model.port" placeholder="请输入端口" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="plc型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="wcs_plc_type" placeholder="请选择plc型号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="系列" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serial">
              <j-dict-select-tag type="list" v-model="model.serial" dictCode="wcs_plc_serial" placeholder="请选择plc系列" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="机架号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rack">
              <a-input-number v-model="model.rack" placeholder="请输入机架号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="插槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slot">
              <a-input-number v-model="model.slot" placeholder="请输入插槽号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="2" style="float: right">
            <a-button icon="save" type="primary"  @click="submitForm">保 存</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="PLC配置明细" key="1">
        <wcs-plc-property-list ref="WcsPlcPropertyList"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import WcsPlcPropertyList from "../WcsPlcPropertyList"

  export default {
    name: 'WcsPlcConfigForm',
    components: {
      WcsPlcPropertyList
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{},
        selectObj:{},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           plcName: [
              { required: true, message: '请输入名称!'},
              { validator: (rule, value, callback) => validateDuplicateValue('wcs_plc_config', 'plc_name', value, this.model.id, callback)},
           ],
        },
        url: {
          add: "/wcs/wcsPlcConfig/add",
          edit: "/wcs/wcsPlcConfig/edit",
          queryById: "/wcs/wcsPlcConfig/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.visible = true;
      },
      edit (record) {
        this.selectObj = record;
        this.model = Object.assign({}, record);
        //加载子列表
        this.$nextTick(() => {
          this.$refs.WcsPlcPropertyList.showDetail(record,this.disabled);
        })
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.selectObj = res.result;
                that.edit(res.result);
                that.$message.success(res.message);
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              //加载子列表
              this.$nextTick(() => {
                this.$refs.WcsPlcPropertyList.showDetail(this.selectObj,this.disabled);
              })
              this.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>